//
// Created by Administrator on 2023/11/24.
//
#include "iostream"

using namespace std;

const int N = 1e5 + 10;
int n, m;
int a[N];

int find(int x) {
    if (a[x] == x) return x;
    a[x] = find(a[x]);
}

void merge(int x, int y) {
    a[find(y)] = find(x);
}

int main() {

    cin >> n >> m;

    for (int i = 1; i <= n; ++i) {
        a[i] = i;
    }

    while (m--) {
        char op;
        int x, y;
        cin >> op >> x >> y;
        if (op == 'M') {
            if (find(x) != find(y)) {
                merge(x, y);
            }
        } else if (op == 'Q') {
            if (find(x) == find(y))cout << "YES" << endl;
            else cout << "NO" << endl;
        }
    }
}